package rtmp;

import amf.AMFUtil;
import user.Receive;
import user.ReceiveGroup;
import util.MsgType;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import lombok.Getter;
import lombok.Setter;

import java.util.List;
import java.util.Map;

public class RtmpNotify  extends Amf{

    @Setter
    @Getter
    private Map<String,Object> data = null;

    public void setNotify(byte[] messageData,String path) {
        setAmfClass(messageData);
        String command = AMFUtil.load_amf_string(amfClass);
        System.err.println(command);
        if (command.equals("@setDataFrame")) {
            String type = AMFUtil.load_amf_string(amfClass);
            System.out.println(type);
            data = AMFUtil.load_amf_mixedArray(amfClass);

            List<Receive> listVideo = ReceiveGroup.getChannel(path);
            if (listVideo != null) {
                for (Receive receive : listVideo) {
                    if (!receive.playing) {
                        continue;
                    }
                    if (receive != null && receive.playing) {
                        ByteBuf byteBuf = ByteBufAllocator.DEFAULT.buffer(1024);
                        System.out.println("keyframe");
                        byteBuf.writeBytes(AMFUtil.writeString("onMetaData"));
                        byteBuf.writeBytes(AMFUtil.writeMixedArray(data));
                        byte[] resultData = new byte[byteBuf.readableBytes()];
                        byteBuf.readBytes(resultData);
                        RtmpResponse.sendData(resultData, MsgType.MSG_NOTIFY, 1337, receive.receive, 0,receive.check_size);
                    }
                }
            }
        }
    }
}
